package com.rc.saas.tenant.service.app;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.mapper.app.TenantAppInfoMapper;
import com.rc.saas.tenant.mapper.app.TenantAppUpgradeMapper;
import com.rc.saas.tenant.model.app.TenantAppInfo;
import com.rc.saas.tenant.model.app.TenantAppInfoExample;
import com.rc.saas.tenant.model.app.TenantAppUpgrade;
import com.rc.saas.tenant.model.app.TenantAppUpgradeExample;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author frog
 * @date 2021/4/15
 */
@Service
public class TenantAppInfoServiceImpl implements TenantAppInfoService {

    @Autowired
    private TenantAppInfoMapper tenantAppInfoMapper;

    @Autowired
    private TenantAppUpgradeMapper tenantAppUpgradeMapper;

    @Override
    public TenantAppInfoMapper getMapper() {
        return tenantAppInfoMapper;
    }

    @Override
    public PageInfo<TenantAppUpgrade> listPageTenantAppInfo(BaseSearch baseSearch) {
        PageHelper.startPage(baseSearch.getOffset(), baseSearch.getLimit());
        TenantAppUpgradeExample example = new TenantAppUpgradeExample();
        example.createCriteria().andTenantCodeEqualTo(baseSearch.getTenantCode());
        List<TenantAppUpgrade> tenantAppUpgrades = tenantAppUpgradeMapper.selectByExample(example);
        return new PageInfo<TenantAppUpgrade>(tenantAppUpgrades);
    }

    @Override
    public void saveTenantInvoiceInfo(TenantAppUpgrade tenantAppUpgrade, String tenantCode) {

        tenantAppUpgrade.setTenantCode(tenantCode);

        TenantAppInfoExample example = new TenantAppInfoExample();
        example.createCriteria().andTenantCodeEqualTo(tenantCode).andAppTypeEqualTo(tenantAppUpgrade.getAppType());
        List<TenantAppInfo> tenantAppInfos = tenantAppInfoMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(tenantAppInfos)) {
            TenantAppInfo tenantAppInfo = new TenantAppInfo();
            BeanUtils.copyProperties(tenantAppUpgrade, tenantAppInfo);
            tenantAppInfo.setTenantCode(tenantCode);
            tenantAppInfoMapper.insertReturnId(tenantAppInfo);

            tenantAppUpgrade.setAppId(tenantAppInfo.getAppId());
            tenantAppUpgradeMapper.insertSelective(tenantAppUpgrade);
        } else {
            tenantAppUpgrade.setAppId(tenantAppInfos.get(0).getAppId());
            tenantAppUpgradeMapper.insertSelective(tenantAppUpgrade);
        }

    }

    @Override
    public void saveReview(Long appUpgradeId, String status) {
        TenantAppUpgrade tenantAppUpgrade = tenantAppUpgradeMapper.selectByPrimaryKey(appUpgradeId);
        if ("1".equals(status)) {
            tenantAppUpgrade.setStatus("2");
        } else if ("0".equals(status)) {
            TenantAppInfo tenantAppInfo = tenantAppInfoMapper.selectByPrimaryKey(tenantAppUpgrade.getAppId());
            tenantAppUpgrade.setStatus("1");
            BeanUtils.copyProperties(tenantAppUpgrade,tenantAppInfo);
            tenantAppInfoMapper.updateByPrimaryKeySelective(tenantAppInfo);
        }
        tenantAppUpgradeMapper.updateByPrimaryKeySelective(tenantAppUpgrade);
    }

}
